﻿package com.cilico.single.components;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.cilico.single.Mid;
import com.cilico.single.R;
import com.cilico.single.utils.Utils;
import com.cilico.single.view.BaseView;
import com.cilico.single.view.DetailView;
import com.cilico.single.view.LoginView;
import com.cilico.single.view.SplashScreen;

/**
 * 警告提示框
 * 
 * @author lv.xiangan
 *
 */
public class Alert {
	public static boolean ISEXIT = true; // 是否退出应用。true:退出；false:不退出。
	String sendLogCmdName_ = "报告错误";
	private Mid mid_;
	private BaseView activity_ = null;
	private String requstUrl_;
    public static Dialog singleAlert_ = null; // 单按钮提示框
    public static Dialog doubleAlert_ = null;  // 双按钮提示框

	/**
	 * Return this display in case the caller is a setCurrent() call embedded by netbeans.
	 */
	public void infoAlert(final Activity activity, Mid mid, final String info, boolean isEnableSend) {
		mid_ = mid;

        if(null != activity){
            final boolean enableSendValue = isEnableSend;
            activity.runOnUiThread(new Runnable() {
                
                @Override
                public void run() {
                 // 弹窗口之前先把存在的dialog取消掉。
                    if(doubleAlert_ != null ){
                        doubleAlert_.dismiss();
                        doubleAlert_ = null;
                    }
                    if(singleAlert_ != null){
                        singleAlert_.dismiss();
                        singleAlert_ = null;
                    }
                    if (!activity.isFinishing()) {
                        WaitDialog.Task.cancelProgressBar((BaseView) activity);
                    }
                    if (enableSendValue) {
                        doubleAlert_ = createDoubleAlert (activity, "提  示", info, sendLogCmdName_, "取消");
                        doubleAlert_.setCancelable(true);
                        ((Button) doubleAlert_.findViewById(R.id.btn_positive)).setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                doubleAlert_.dismiss();
                            }
                        });

                        ((Button) doubleAlert_.findViewById(R.id.btn_negative)).setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                doubleAlert_.dismiss();
                            }
                        });
                        if (!activity.isFinishing() && !doubleAlert_.isShowing()) {
                                doubleAlert_.show();
                        }
                    } else {
                        final boolean isExit = ISEXIT;
                        if (!ISEXIT) {
                            ISEXIT = true;
                        }
                        singleAlert_ = createSingleAlert (activity, "提  示", info, "确定");
                        singleAlert_.setCancelable(true);
                        ((Button) singleAlert_.findViewById(R.id.btn_ok)).setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                            	singleAlert_.dismiss();
        						if (activity instanceof SplashScreen) {
        							// 如果在闪屏界面跳出该异常，则直接提时候退出程序。
        							if (isExit) {
        								((BaseView)activity).exitApplication();
        							}
        						}
                            }
                        });

                        if (!activity.isFinishing() && !singleAlert_.isShowing()) {
                                singleAlert_.show();
                        }
                    }
                }
            });
        }
	}

	/**
	 * 退出系统
	 * 
	 * @param activity
	 */
    public static void exitSystem(final BaseView activity) {
    	String info = activity.getString(R.string.exit_prompt);
        doubleAlert_ = createDoubleAlert (activity, "提  示", info, "确定", "取消");
        doubleAlert_.setCancelable(true);
        ((Button) doubleAlert_.findViewById(R.id.btn_positive)).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                WaitDialog.Task.cancelProgressBar(activity);
                // 一般是采用finish方法，但是在转换wifi和wap连接的时候会出现系统提示连接长时间死亡的情况。
                activity.exitApplication();
                doubleAlert_.dismiss();
            }
        });

        ((Button) doubleAlert_.findViewById(R.id.btn_negative)).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                doubleAlert_.dismiss();
            }
        });
        if (!activity.isFinishing() && !doubleAlert_.isShowing()) {
                doubleAlert_.show();
        }
    }
	
	
	/**
	 * 后台定义弹出筐，有请求动作
	 */
	public void actionInfoAlert(final BaseView activity, Mid mid, String title, String info, String cmdStr, final String url) {
		final Builder ad = new AlertDialog.Builder(activity);
		activity_ = activity;
		mid_ = mid;
		requstUrl_ = url;
		
		if (info == null) {
			info = " ";
		}
		if (title == null) {
			title = "提示";
		}
		String[] cmdStrArray = null;
		if (cmdStr != null) {
			cmdStrArray = cmdStr.split(",");
		}
		ad.setTitle(title).setMessage(info).setCancelable(true);
		if (cmdStr != null && cmdStrArray.length > 0) {
			ad.setPositiveButton(cmdStrArray[0], new DialogInterface.OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					if(null != requstUrl_) {
						String tempUrl[] = null;
						if (null != requstUrl_){
							tempUrl = requstUrl_.split(",");
						}
						if (tempUrl != null && tempUrl[0].length() > 0){
							requstUrl_ = tempUrl[0];
						}
						alertCommandAction(0);
					}
				}
			});
			if (cmdStrArray.length > 1) {
				ad.setNegativeButton(cmdStrArray[1], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface arg0, int arg1) {
						String tempUrl[] = null;
						if (null != requstUrl_){
							tempUrl = requstUrl_.split(",");
						}
						if (tempUrl != null && tempUrl.length == 2 && tempUrl[1].length() > 0){
							requstUrl_ = tempUrl[1];
							alertCommandAction(0);
						} else{
							alertCommandAction(1);
						}
					}
				});
			}
		}
		
		if (null != activity) {
			activity.runOnUiThread(new Runnable() {
				public void run() {
					if (!activity.isFinishing()) {
						ad.show();
					}
				}

			});
		}
	}
	
	private void alertCommandAction(int action) {
		switch (action) {
		case 0:
			if (requstUrl_ != null && requstUrl_.length() > 0) {
				Utils.printOutToConsole("requstUrl_ = " + requstUrl_);
				requstUrl_ = requstUrl_.replaceAll("lp", "http");
				requstUrl_ = requstUrl_.replaceAll("&amp;", "&");
				int index = requstUrl_.indexOf('?') + 1;
				String body = requstUrl_.substring(index);
				String host = requstUrl_.substring(0, index);
				Utils.printOutToConsole("host = " + host + "\nbody = " + body);
				if (activity_ instanceof DetailView){
					mid_.channelEntity_.sendForm(host, body, false, activity_, false);
				} else if (activity_ instanceof LoginView){
					mid_.processLogin(host, body, false, activity_, null);
				}
			}
			break;
		case 1:
			Utils.printOutToConsole("check cannel!!!!!!!!!!!!");
			break;
		}
	}

	
    /** 
     * 创建单按钮对话框
     * @param activity
     * @param title
     * @param info
     * @param btn
     * @return
     */
    public static Dialog createSingleAlert (Activity activity, String title, String info, String btn) {
        if(singleAlert_ == null){
            singleAlert_ = new Dialog(activity, android.R.style.Theme_Translucent_NoTitleBar);
            singleAlert_.requestWindowFeature(Window.FEATURE_NO_TITLE);
            singleAlert_.setContentView(R.layout.alert_layout_single);
        }
        singleAlert_.dismiss();
        ((TextView) singleAlert_.findViewById(R.id.dialog_title)).setText(title);
        ((TextView) singleAlert_.findViewById(R.id.dialog_info)).setText(info);
        ((Button) singleAlert_.findViewById(R.id.btn_ok)).setText(btn);
        return singleAlert_;
    }

    /**
     * 创建双按钮对话框
     * @param activity
     * @param title
     * @param info
     * @param btn1
     * @param btn2
     * @return
     */
    public static Dialog createDoubleAlert (Activity activity, String title, String info, String btn1, String btn2) {
    	doubleAlert_ = new Dialog(activity, android.R.style.Theme_Translucent_NoTitleBar);
    	doubleAlert_.requestWindowFeature(Window.FEATURE_NO_TITLE);
    	doubleAlert_.setContentView(R.layout.alert_layout_double);
//    	if(doubleAlert_ == null){
//        }
//        doubleAlert_.dismiss();
        ((TextView) doubleAlert_.findViewById(R.id.dialog_title)).setText(title);
        ((TextView) doubleAlert_.findViewById(R.id.dialog_info)).setText(info);
        ((Button) doubleAlert_.findViewById(R.id.btn_positive)).setText(btn1);
        ((Button) doubleAlert_.findViewById(R.id.btn_negative)).setText(btn2);
        return doubleAlert_;
    }
    
    
}
